package com.organicsalt.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @ClassName: SwaggerConfig
 * @Description: swagger配置类
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /** 创建API */
    @Bean
    public Docket createRestApi() {

        // 添加head参数配置start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        // 添加head参数配置end
        return new Docket(DocumentationType.SWAGGER_2)
                // 是否开启
                .enable(true)
                // 详细定制
                .apiInfo(apiInfo()).select()
                // 指定当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.organicsalt.springboot.controller"))
                // 扫描所有 .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build()
                .globalOperationParameters(pars);// 注意这里
    }

    /** 添加摘要信息 */
    private ApiInfo apiInfo() {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("前后端调试用")
                .contact(new Contact(null, null, null))
                .version("版本号:1.0.1" ).build();
    }
}
